package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
        topic = "tanhua-publish",consumerGroup = "tanhua-publish-consumer"
)
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate template;

    @Override
    public void onMessage(String id) {
        System.out.println("开始审核动态内容id="+id);
        Publish publish = publishApi.findById(id);

        //1、只会对状态=0的动态，进行内容审核
        if(publish.getState() == 0) {
            //2、调用template工具，审核文字内容
            boolean textCheck = template.textContentCheck(publish.getTextContent());
            //3、调用template工具，审核图片内容
            boolean imageCheck = template.imageContentCheck(publish.getMedias().toArray(new String[]{}));//传递图片的所有URL数组
            //4、根据审核结果更新publish状态
            Integer state = 2; //审核驳回
            if(textCheck && imageCheck) {
                state = 1;
            }
            publishApi.updateState(id,state);
        }
    }
}
